<template>
    <div class='readCard'>
        <el-button type="primary" @click="btnClicks">读取</el-button>
        <iframe :src="src" style="height:0px;position:absolute;top:-1050px" id="readCards" name="readCards"></iframe>
    </div>
   
</template>
<script>
import HighShot  from './HighShot'
export default {
    props:['id'],
    name:'read-card',
    components: {
        HighShot
    },
    data(){
        return{
            src:''
        }
    },
    created(){
        this.src=localStorage.getItem("ip")+"apiServe/highShot";
        let sef=this;
        try{
            window.addEventListener('message', function(messageEvent) {
                var data = messageEvent.data;
                if(data&&typeof(data)=='string'&&data.indexOf("{")!=-1){
                    let item=JSON.parse(data);
                    let ids=sef.id.split("|");
                    let age="";
                    for(let i=0;i<ids.length;i++){
                        let id_=ids[i].split(":");
                        let datas={};
                        let val=item[id_[0]];
                        if(id_[0]=="mz"){
                            val=val+"族";
                        }
                        if(id_[0]=="Born"&&val){
                            let year=val.substring(0,4);
                            let month=val.length>6 ? val.substring(4,6):"";
                            let day=val.length==8 ? val.substring(6):"";
                            val=year+"-"+month+"-"+day;
                                                
                            var date = new Date();
                            let curYear=date .getFullYear(); //获取完整的年份(4位)
                            age=Number(curYear)-Number(year);
                            age=age.toFixed(0);

                        }
                        if(id_[0]=="age")
                            val=age;
                        datas.id=id_[1];
                        datas.val=val;

                        sef.$emit("input",JSON.stringify(datas));
                    }
                }
                
            },false);
        }catch(e){}
    },
    methods:{
        btnClicks(){
            
            window.readCards.postMessage("readCard",'*');
        }
    }
}
</script>
<style scoped>
   .readCard{
       display:inline-block;
       text-align: center;
       margin-left:10px;
   }
</style>